package com.ios.palette.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ios.palette.domain.User;
import com.ios.palette.respVO.Result;
import com.ios.palette.domain.Status;
import com.ios.palette.service.IUserService;
import com.ios.palette.service.IStatusService;
import com.ios.palette.utils.Upload;

/**
 * @author 志刚
 *
 */
@Controller
@RequestMapping(value = "/user/")
public class UserController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IStatusService statusService;

	/**
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return result{status 状态码,msg 信息,data 数据}
	 */
	@RequestMapping(value = "login")
	@ResponseBody
	public Result login(@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "password", required = true) String password, HttpServletRequest request)
					throws Exception {
		Result result = new Result("201", "用户名或密码错误", null);
		if (username != null && password != null) {
			User user = userService.get(username);
			if (user == null) {
				result.setMsg("此用户不存在");
				return result;
			}
			if (password.equals(user.getPassword())) {
				result.setStatus("200");
				result.setMsg("登陆成功");
				result.setData(user);
				request.getSession().setAttribute("user", user);
			}
		}
		return result;
	}

	/**
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return result {status 状态码,msg 信息,data 数据}
	 */
	@RequestMapping(value = "register")
	@ResponseBody
	public Result register(String username, String password) throws Exception {
		Result result = new Result("201", "注册失败", null);
		User user = new User();
		if (userService.get(username) != null) {
			result.setMsg("用户名已存在");
			return result;
		}
		user.setUsername(username);
		user.setPassword(password);
		if (userService.add(user) != null) {
			result.setStatus("200");
			result.setMsg("注册成功");
			result.setData(user);
		}
		return result;
	}

	/**
	 * @param user
	 *            JOSN类User对象
	 * @param image
	 *            头像文件
	 * @param request
	 * @return result 更新后的用户对象
	 * @throws Exception
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Result update(@RequestBody User user, @RequestParam MultipartFile image, HttpServletRequest request)
			throws Exception {
		Result result = new Result("201", "修改失败", null);
		if (!image.isEmpty()) {
			Upload upload = new Upload();
			String imagePath = upload.uploadAndGetPath(image, request);
			user.setImage(imagePath);
		}
		userService.update(user);
		if (user != null) {
			result.setStatus("200");
			result.setMsg("修改成功");
			result.setData(user);
		}
		return result;
	}

	/**
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "logout")
	@ResponseBody
	public Result logout(HttpServletRequest request) throws Exception {
		Result result = new Result("200", "注销错误", null);
		request.getSession().removeAttribute("user");
		result.setStatus("201");
		result.setMsg("注销成功");
		return result;
	}

	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addConcern")
	@ResponseBody
	public Result addConcern(@RequestBody User user, HttpServletRequest request) throws Exception {
		Result result = new Result("201", "关注失败", user);
		if (user != null) {
			User fans = (User) request.getSession().getAttribute("user");
			fans.getConcern().add(user);
			user.getFans().add(fans);
			userService.update(user);
			userService.update(fans);
			result.setStatus("200");
			result.setMsg("关注成功");
			result.setData(fans);
		}
		return result;
	}

	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "removeConcern")
	@ResponseBody
	public Result removeConcern(@RequestBody User user, HttpServletRequest request) throws Exception {
		Result result = new Result("201", "关注删除失败", user);
		if (user != null) {
			User fans = (User) request.getSession().getAttribute("user");
			fans.getConcern().remove(user);
			user.getFans().remove(fans);
			userService.update(user);
			userService.update(fans);
			result.setStatus("200");
			result.setMsg("关注删除成功");
			result.setData(fans);
		}
		return result;
	}

	/**
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addCollection")
	@ResponseBody
	public Result addCollection(@RequestBody Status status, HttpServletRequest request) {
		Result result = new Result("201", "收藏失败", status);
		if (status != null) {
			User user = (User) request.getSession().getAttribute("user");
			user.getCollection().add(status);
			status.setCollectionCount(status.getCollectionCount() + 1);
			userService.update(user);
			statusService.update(status);
			result.setStatus("200");
			result.setMsg("收藏成功");
			result.setData(user);
		}
		return result;
	}

	/**
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "removeCollection")
	@ResponseBody
	public Result removeCollection(@RequestBody Status status, HttpServletRequest request) {
		Result result = new Result("201", "收藏删除失败", status);
		if (status != null) {
			User user = (User) request.getSession().getAttribute("user");
			user.getCollection().remove(status);
			status.setCollectionCount(status.getCollectionCount() - 1);
			userService.update(user);
			statusService.update(status);
			result.setStatus("200");
			result.setMsg("收藏删除成功");
			result.setData(user);
		}
		return result;
	}

	@RequestMapping(value = "search")
	@ResponseBody
	public Result search(String username) {
		Result result = new Result("201", "搜索失败", null);
		List<User> list = userService.searchByName(username);
		if (list != null) {
			result.setStatus("200");
			result.setMsg("搜索成功");
			result.setData(list);
		} else {
			result.setStatus("200");
			result.setMsg("搜索结果为空");
			result.setData(list);
		}
		return result;
	}

	@RequestMapping(value = "getUser")
	@ResponseBody
	public Result getUser(String id) {
		Result result = new Result("201", "查找失败", null);
		User user = userService.get(User.class, id);
		if (user == null) {
			result.setMsg("用户不存在");
			result.setData(user);
		} else {
			result.setStatus("200");
			result.setMsg("查找成功");
			result.setData(user);
		}
		return result;
	}

}
